package edu.zqu.collegeField.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCalendarDateApi;
import edu.zqu.collegeField.framework.openfeign.api.GetCalendarIdApi;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.framework.openfeign.api.GetTimeTableNameApi;
import edu.zqu.collegeField.project.dto.FieldDto;
import edu.zqu.collegeField.project.dto.FieldStatusDto;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldStatus;
import edu.zqu.collegeField.project.mapper.FieldStatusMapper;
import edu.zqu.collegeField.project.service.FieldStatusService;
import edu.zqu.collegeField.project.vo.FieldStatusVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 场地状态 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldStatusServiceImpl extends ServiceImpl<FieldStatusMapper, FieldStatus> implements FieldStatusService {
    @Autowired
    private FieldStatusMapper fieldStatusMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;
    @Autowired
    private GetCalendarIdApi getCalendarIdApi;
    @Autowired
    private GetCalendarDateApi getCalendarDateApi;
    @Autowired
    private GetTimeTableNameApi getTimeTableNameApi;

    @Override
    public List<FieldStatusVo> get(BigInteger collegeId,//获取场地状态列表
                                   BigInteger schoolId,
                                   BigInteger areaId,
                                   BigInteger fieldId,
                                   BigInteger fieldTypeId,
                                   BigInteger calendarId,
                                   BigInteger timeTableId,
                                   Integer status) {
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("schoolId", Convert.toStr(schoolId));
        map.put("areaId", Convert.toStr(areaId));
        map.put("fieldId", Convert.toStr(fieldId));
        map.put("field_typeId", Convert.toStr(fieldTypeId));
        map.put("calendarId", Convert.toStr(calendarId));
        map.put("timeTableId", Convert.toStr(timeTableId));
        map.put("name", Convert.toStr(status));
        List<FieldStatusVo> fieldStatusVoList = BeanMapper.mapList(fieldStatusMapper.get(map), FieldStatusVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldStatusVo fieldStatusVo : fieldStatusVoList) {
            fieldStatusVo.setCollegeName(collegeName);
            fieldStatusVo.setCalendarDate(getCalendarDateApi.getCalendarDate(fieldStatusVo.getCalendarId()))
            .setTimeTableName(getTimeTableNameApi.getTimeTableName(fieldStatusVo.getTimeTableId()));
        }
        return fieldStatusVoList;
    }

    @Override
    public FieldStatusVo getById(BigInteger id) {//获取场地状态信息
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldStatusVo fieldStatusVo = BeanMapper.map(fieldStatusMapper.getById(map), FieldStatusVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldStatusVo.getCollegeId());
        fieldStatusVo.setCollegeName(collegeName);
        return fieldStatusVo;
    }

    @Override
    public int add(FieldStatus fieldStatus) {//添加场地状态信息
        fieldStatus.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldStatusMapper.insert(fieldStatus);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldStatus fieldStatusUpdate) {
        FieldStatus fieldStatus = fieldStatusMapper.selectById(fieldStatusUpdate.getId());
        if (StringUtils.isNull(fieldStatus)) {
            throw new NullPointerException("找不到该场地状态");
        }
        BeanMapper.copyPropertiesNotNull(fieldStatusUpdate, fieldStatus);
        try {
            int insert = fieldStatusMapper.insert(fieldStatus.setLastVersion(fieldStatusUpdate.getId()));
            if (insert >= 1) {
                return delete(fieldStatusUpdate.getId(), fieldStatusUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {//删除场地状态信息
        try {
            UpdateWrapper<FieldStatus> fieldStatusUpdateWrapper = new UpdateWrapper<FieldStatus>().eq("id", Convert.toStr(id));
            int update = fieldStatusMapper.update(new FieldStatus().setByWho(byWho), fieldStatusUpdateWrapper);
            if (update >= 1) {
                return fieldStatusMapper.delete(fieldStatusUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
